<?php
/*
  Project: CIForum
  Type: PHP
  Desc: Contains all functions related to ADMIN Control Panel.
  Author: Yashpal
  Usage:  	call admin/{functioname}
			

  Function name : 
  post_thread   -> This function will post thread.
  post_forum    -> This function will post reply.
  post_category -> This function will create category.
  some new

*/

class Admin extends Controller {

    function Admin() {
        parent::Controller();
        $this->load->library('YB_Admin');
    }

//new data


    function index() {
        $data['title']='forum';
        $data['sidelinks']='side links';
        $data['content']=$this->load->view('admin/dashboard',$data,TRUE);
        $this->load->view('admin/main',$data);
    }


    function manage_settings() {
        $data['title']='Create Category - Forum';
        $data['content']=$this->load->view('admin/settings',$data,TRUE);
        $this->load->view('admin/main',$data);
    }

    //****************************************************************************************
    //user management
    //****************************************************************************************

    function edit_user($member_id,$page_id=1) {
        $rules['member_name'] = "trim|required|min_length[4]|max_length[20]";
        $rules['member_email'] = "trim|required|valid_email|max_length[100]";
        $rules['password'] = "trim|min_length[6]|max_length[20]";
        $this->validation->set_rules($rules);

        if ($this->validation->run()) {

            $this->Adminusersmodel->edit_user($member_id,$page_id);
        }
        else {
            $this->db->where(array('member_id'=>$member_id));
            $query=$this->db->get('board_members');
            $data['user']=$query->row_array();
            $data['title']="Edit User -MyBlog";
            $this->load->view('admin/users/edit_member',$data);
        //$this->load->view('admin/edit_user',$data);
        }
    }



    function add_user() {

        $rules['member_name'] = "trim|required|min_length[4]|max_length[20]|callback__check_username";
        $rules['member_password'] = "trim|required|min_length[6]|max_length[20]";
        $rules['member_email'] = "trim|required|valid_email|max_length[100]";
        $this->validation->set_rules($rules);



        $fields['member_name'] = 'Member Name';
        $fields['display_name'] = 'Display Name';
        $fields['member_password'] = 'Password';
        $fields['member_email'] = 'Email ID';
        $fields['legends'] = 'Legends';
        $fields['member_group'] = 'Member Group';
        $fields['member_photo'] = 'Member Photo';
        $fields['member_url'] = 'URL - Website link';
        $fields['buddy_list'] = 'Buddy List';
        $fields['ignore_list'] = 'Ignore List';
        $fields['blocked_list'] = 'Blocked List';
        $fields['new_pm'] = 'New PM';
        $fields['aol_im'] = 'AOL IM';
        $fields['icq'] = 'ICQ';
        $fields['yahoo_messenger'] = 'Yahoo Messenger';
        $fields['msn_messenger'] = 'MSN Messenger';
        $fields['location'] = 'Location';
        $fields['occupation'] = 'Occupation';
        $fields['interests'] = 'Intrests';
        $fields['statistics'] = 'Statistics';
        $fields['member_thread'] = 'Member Thread';
        $fields['member_replies'] = 'Member Replies';
        $fields['last_visit'] = 'Last Visit';
        $fields['local_time'] = 'Local Time';
        $fields['join_date'] = 'Join Date';
        $fields['recent_reply'] = 'Recent Reply';
        $fields['recent_thread'] = 'Recent Thread';
        $fields['total_filter'] = 'Total Filter';
        $fields['total_folders'] = 'Total Folders';
        $fields['birthday_date'] = 'Birthday';
        $fields['bio'] = 'Bio';
        $this->validation->set_fields($fields);

        if ($this->validation->run()) {
            $this->Adminusersmodel->insert_user();
        }
        else {
            $data['title']="Add a user - MyBlog";
            $this->load->view('admin/users/add_member',$data);

        }



    }

    function manage_users($pageid=1) {

    //$login = $this->session->userdata('admin_logged_in');
    //if(!$login)
    // redirect('admin/login','refresh'); }

        $data['title']="Users Management - MyBlog";
        $data['title']='forum';
        $data['sidelinks']='side links';
        $data['content']=$this->Adminusersmodel->ListAllUsers($pageid);
        $this->load->view('admin/main',$data);



    }

    function delete_user($user_id,$page_id) {
        $this->Adminusersmodel->delete_user($user_id,$page_id);
    }


    //************************************************************************
    //thread management
    //************************************************************************
    function manage_posts($pageid=1) {
        $data['content']=$this->Postmodel->list_all_posts($pageid);
        $data['title']='forum';
        $this->load->view('admin/main',$data);
    }


    function delete_post($post_id,$page_id) {
        $this->Postmodel->delete_post($post_id,$page_id);
    }

    function edit_post($post_id,$page_id=1) {
        $rules['post_content'] = "required";
        $this->validation->set_rules($rules);

        $fields['post_content'] = 'Post content';
        $fields['enable_emoticons'] = 'emoticons';
        $fields['uploaded_files'] = 'upload';
        $fields['enable_signature'] = 'signature';
        $fields['enable_notification'] = 'notification';
        $fields['reason_content'] = 'reason';
        $fields['icon_id'] = 'icon id';
        $this->validation->set_fields($fields);


        if (empty($_POST)) {
            $this->db->where(array('post_id'=>$post_id));
            $query=$this->db->get('board_posts');
            $post=$query->row_array();
            $this->validation->post_content = $post['post_content'];
            $this->validation->reason_content = $post['reason_content'];

            if($post['enable_emoticons']==1)
                $this->validation->set_checkbox('enable_emoticons', '1');
            else
                $this->validation->set_checkbox('enable_emoticons', '0')	;

            if($post['enable_signature']==1)
                $this->validation->set_checkbox('enable_signature', '1');
            else
                $this->validation->set_checkbox('enable_signature', '0');

            if($post['enable_notification']==1)
                $this->validation->set_checkbox('enable_notification', '1');
            else
                $this->validation->set_checkbox('enable_notification', '0');

            $this->validation->icon_id = $post['icon_id'];


        }

        if ($this->validation->run()) {
            $this->Postmodel->edit_post($post_id,$page_id);
        }
        else {
            $data['title']="Edit Posts - Forum";
            $data['content']=$this->load->view('users/edit_reply',$data,TRUE);
            $this->load->view('admin/main',$data);
        }
    }


    //Thread functions
    function manage_threads($pageid=1) {
        $data['title']="Thread Management - Forum";
        $data['content']=$this->Threadmodel->list_all_threads($pageid);
        $this->load->view('admin/main',$data);
    }


    function delete_thread($thread_id,$page_id) {
        $this->Threadmodel->delete_thread($thread_id,$page_id);
    }

    function edit_thread($thread_id,$page_id=1) {
        $rules['thread_content'] = "required";
        $rules['thread_title'] = "required";
        $this->validation->set_rules($rules);

        $fields['thread_content'] = 'thread content';
        $fields['uploaded_files'] = 'uploaded';
        $fields['thread_title'] = 'thread title';
        $fields['thread_desc'] = 'thread desc';
        $fields['make_announcement'] = 'announcement';
        $fields['open_date'] = 'open date';
        $fields['close_date'] = 'close date';
        $fields['meta'] = 'meta';
        $fields['enable_emoticons'] = 'emoticons';
        $fields['enable_signature'] = 'signature';
        $fields['enable_notification'] = 'notification';
        $fields['reason_content'] = 'reason';
        $fields['icon_id'] = 'icon id';
        $fields['keyword'] = 'keyword';
        $fields['description'] = 'desc';
        $this->validation->set_fields($fields);

        if (empty($_POST)) {
            $this->db->where(array('thread_id'=>$thread_id));
            $query=$this->db->get('board_threads');
            $post=$query->row_array();

            $this->validation->thread_content = $post['thread_content'];
            $this->validation->thread_title = $post['thread_title'];
            $this->validation->thread_desc = $post ['thread_desc'];
            $this->validation->meta = $post ['meta'];
            $this->validation->keyword = $post ['keyword'];
            $this->validation->description = $post ['description'];
            if($post['enable_emoticons']==1)
                $this->validation->set_checkbox('enable_emoticons', '1');
            else
                $this->validation->set_checkbox('enable_emoticons', '0')	;

            if($post['enable_signature']==1)
                $this->validation->set_checkbox('enable_signature', '1');
            else
                $this->validation->set_checkbox('enable_signature', '0');


            if($post['enable_notification']==1)
                $this->validation->set_checkbox('enable_notification', '1');
            else
                $this->validation->set_checkbox('enable_notification', '0');

            $this->validation->icon_id = $post['icon_id'];


        }

        if ($this->validation->run()) {
            $this->Threadmodel->edit_thread($thread_id,$page_id);
        }
        else {
            $data['title']="Edit Thread - Forum";
            $data['content']=$this->load->view('users/edit_thread',$data,TRUE);
            $this->load->view('admin/main',$data);

        }
    }

    //************************************************************************
    //forum management chan
    //************************************************************************

    function create_forum() {


        $form=$this->form_validation;
        $form->set_rules('forum_title', 'Forum Title', 'required');
        $form->set_rules('forum_desc', 'Forum Description', 'required');
        $form->set_rules('category', 'Category', 'required');

        if (!$form->run()) {
            $data['title']='Create Forum';
            $cat['categories_list']=$this->yb_admin->list_categories();
            $data['content']=$this->load->view('admin/forum/create_forum',$cat,TRUE);

            $this->load->view('admin/main',$data);

        }
        else {
        //save forum using yb_admin
            $this->yb_admin->create_forum();
        }

    }  	//end of post_forum


    function manage_forums($pageid=1) {
        $data['title']="Forum Management - Forum";
        $data['content']=$this->yb_admin->manage_forums($pageid);
        $this->load->view('admin/main',$data);
    }


    function delete_forum($forum_id,$page_id) {
        $this->yb_admin->delete_forum($forum_id,$page_id);
    }

    function edit_forum($post_id,$page_id=1) {

        $form=$this->form_validation;
        $form->set_rules('forum_title', 'Forum Title', 'required');
        $form->set_rules('forum_desc', 'Forum Description', 'required');
        $form->set_rules('category', 'Category', 'required');


        if (empty($_POST)) {
            $this->db->where(array('forum_id'=>$post_id));
            $query=$this->db->get('board_forums');
            $post=$query->row_array();


            $forum['forum_title'] = $post['forum_title'];
            $forum['forum_desc']  = $post['forum_desc'];
            $forum['category_title']   = $post['cat_title'];
        }

        if ($form->run()) {
            $this->yb_admin->edit_forum();
        }
        else {
            $data['title']="Edit Forum - Forum";
            $forum['categories_list']=$this->yb_admin->list_categories();
            $data['content']=$this->load->view('admin/forum/edit_forum',$forum,TRUE);
            $this->load->view('admin/main',$data);
        }
    }

    /************************************************************************
		CATEGORY Management
    //************************************************************************/

    function create_category() {

        $form=$this->form_validation;
        $form->set_rules('category_title', 'Category Title', 'required');
        $form->set_rules('category_desc', 'Category Description', 'required');

        if (!$form->run()) {
            $data['title']='Create Category - Forum';
            $data['content']=$this->load->view('admin/category/create_category',$data,TRUE);
            $this->load->view('admin/main',$data);
        }
        else {
            $this->yb_admin->create_category();
        }

    }	//end of post_category

    function manage_categories($page_id=1) {
        $data['title']="category Management - Forum";
        $data['content']= $this->yb_admin->manage_categories($page_id);
        $this->load->view('admin/main',$data);
    }

    function delete_category() {
        $this->yb_admin->delete_category();
    }

    function edit_category($category_id,$page_id=1) {
        $form=$this->form_validation;
        $form->set_rules('category_title', 'Category Title', 'required');
        $form->set_rules('category_desc', 'Category Description', 'required');

        if (empty($_POST)) {

            $this->db->where(array('category_id'=>$category_id));
            $query=$this->db->get('board_categories');
            $post=$query->row_array();


            $datab['category_title'] = $post['category_title'];
            $datab['category_desc']  = $post['category_desc'];

        }

        if ($form->run()) {
            $this->yb_admin->edit_category();
        }
        else {

            $data['title']="Edit category - Forum";
            $data['content']=$this->load->view('admin/category/edit_category',$datab,TRUE);
            $this->load->view('admin/main',$data);
        }
    }

}

/* End of file admin.php */
/* Location: ./system/application/controllers/admin.php */?>